package com.deng.maintenance.dao.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.vo.DispenseOrganizationVo;
import com.deng.common.vo.OrganizationVo;
import com.deng.maintenance.dao.IEditItemWeight;
import com.deng.maintenance.vo.ItemWeightVo;
import com.deng.util.DateTools;

public class EditItemWeightImpl 
		extends SQLHelper
		implements IEditItemWeight{
	
	
	public List<ItemWeightVo> getAllItemWeight() {
		// TODO Auto-generated method stub
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,"select * from UNIT_PRICE_ITEM_WEIGHT " +
					"order by item_type_name,item_name");
			List<ItemWeightVo> list = new ArrayList<ItemWeightVo>();
			while(rs.next()){
				ItemWeightVo vo = new ItemWeightVo();
				vo.wid = rs.getInt("id");
				vo.item_name = rs.getString("item_name");
				vo.item_type_name = rs.getString("item_type_name");
				vo.unit_price = rs.getBigDecimal("price").toString();
				vo.operate_weight = rs.getBigDecimal("operate_weight").multiply(new BigDecimal(100)).toString();
				vo.order_weight = rs.getBigDecimal("order_weight").multiply(new BigDecimal(100)).toString();
				vo.item_id = rs.getString("item_id");
				vo.item_type = rs.getString("item_type");
				vo.point = rs.getInt("point")+"";
				list.add(vo);
			}
			freeConnection(conn);
			return list;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	public int addItem(ItemWeightVo vo) {
		try{
			Connection con = getConnect();
			ResultSet rs = queryData(con,"select case when max(id) is not null then max(id)+1 else 1 end as id" +
					" from UNIT_PRICE_ITEM_WEIGHT");			
			int maxID = 0;
			if(rs.next()){
				maxID = rs.getInt("id");
			}
			rs = queryData(con,"select * from UNIT_PRICE_ITEM_WEIGHT where item_id = '"+vo.item_id+"'");	
			if(rs.next()){
				return 0;
			}
			
			int x = updateData(con,"insert into UNIT_PRICE_ITEM_WEIGHT " +
					"	(id,item_id,item_name,create_time,operate_weight)" +
					"	values("+maxID+",'"+vo.item_id+"','"+vo.item_name+"','"+DateTools.dtsWithTime(new Date())+"',1)");
			freeConnection(con);
			return x;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return 0;
	}
	public int updateAllItemWeight(List<ItemWeightVo> list) {
		// TODO Auto-generated method stub
		try{
			for(ItemWeightVo vo:list){
				if(vo.update_mark == 1){
					Connection conn = getConnect();
					updateData(conn,"update UNIT_PRICE_ITEM_WEIGHT " +
							" set order_weight = "+new BigDecimal(vo.order_weight).divide(new BigDecimal(100))+"," +
							" operate_weight = "+new BigDecimal(vo.operate_weight).divide(new BigDecimal(100))+"," +
							" point = "+vo.point+
							" where id = "+vo.wid);
					freeConnection(conn);
				}
			}
			return 1;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return 0;
	}

	public int updateItemWeight(ItemWeightVo vo) {
		try{
			Connection conn = getConnect();
			updateData(conn,"update UNIT_PRICE_ITEM_WEIGHT " +
					" set order_weight = "+new BigDecimal(vo.order_weight).divide(new BigDecimal(100))+"," +
					" operate_weight = "+new BigDecimal(vo.operate_weight).divide(new BigDecimal(100))+"," +
					" point = "+vo.point+
					" where id = "+vo.wid);
			freeConnection(conn);
			return 1;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return 0;
	}
	public void batReplacePoint(List<ItemWeightVo> itemList,
			List<DispenseOrganizationVo> orgList) {
		try
		{
			Connection conn = getConnect();
			ResultSet rs ;
			String sql ;
			for(ItemWeightVo itemVo:itemList)
			{
				for(DispenseOrganizationVo ovo:orgList)
				{
					System.out.println(itemVo.item_id);
					System.out.println(ovo.id);
					
					rs = queryData(conn, "select count(*) from UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION" +
							"	where item_id = '"+(itemVo.item_id)+"' and organization_id = "+ovo.id);
					if(rs.next())
					{
						int count = rs.getInt(1);
						if(count > 1)
						{
							if(itemVo.order_weight.length() != 0)
							{
								sql = "update UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION" +
									"	set order_point = "+itemVo.order_weight+
									"	where item_id = '"+(itemVo.item_id)+"'" +
									"	and organization_id = "+ovo.id+
									"	and order_point is not null";
								updateData(conn, sql);
							}
							if(itemVo.operate_weight.length() != 0)
							{
								sql = "update UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION" +
									"	set operate_point = "+itemVo.operate_weight+
									"	where item_id = '"+(itemVo.item_id)+"'" +
									"	and organization_id = "+ovo.id+
									"	and operate_point is not null";
								updateData(conn, sql);
							}
						}
						else if(count == 1)
						{
							String tmp = "";
							if(itemVo.order_weight.length() != 0)
							{
								tmp += " set order_point = "+itemVo.order_weight;								
								
							}
							if(itemVo.operate_weight.length() != 0)
							{
								tmp += (tmp.length()== 0?" set ":",")+"operate_point = "+itemVo.operate_weight;	
							}
							sql = "update UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION" +
								tmp+
								"	where item_id = '"+(itemVo.item_id)+"'" +
								"	and organization_id = "+ovo.id;
							updateData(conn, sql);
						}
						else
						{
							sql = "insert into UNIT_PRICE_ITEM_INCOME_VS_ORGANIZATION" +
								" (organization_id,item_id,point,order_point,operate_point,is_important,duty_id)" +
								" values("+ovo.id+",'"+itemVo.item_id+"',0,"+
								(itemVo.order_weight.equals("")?"0":itemVo.order_weight)+","+
								(itemVo.operate_weight.equals("")?"0":itemVo.operate_weight)+",1,"+ovo.duty_id+")";
							updateData(conn, sql);
						}
					}
				}
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public List<ItemWeightVo> getPartsWeight(String name, String type) {
		// TODO Auto-generated method stub
		try
		{
			String sql = "select * from UNIT_PRICE_ITEM_WEIGHT ";
			String end = "";
			if(name != null && name.trim().length() > 0)
			{
				end += (end.length() == 0?" where ":" and ")+" item_name like '%"+name+"%' ";
			}
			if(!type.equals("0"))
			{
				end += (end.length() == 0?" where ":" and ")+" item_type = '"+type+"' ";
			}
			sql += end+ "  order by item_type_name,item_name ";
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,sql);
			List<ItemWeightVo> list = new ArrayList<ItemWeightVo>();
			while(rs.next()){
				ItemWeightVo vo = new ItemWeightVo();
				vo.wid = rs.getInt("id");
				vo.item_name = rs.getString("item_name");
				vo.item_type_name = rs.getString("item_type_name");
				vo.unit_price = rs.getBigDecimal("price").toString();
				vo.operate_weight = "";
				vo.order_weight = "";
				vo.item_id = rs.getString("item_id");
				vo.item_type = rs.getString("item_type");
				vo.point = rs.getInt("point")+"";
				list.add(vo);
			}
			freeConnection(conn);
			return list;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return null;
	}

	
}
